package com.example.mytestdemo.car.aidl;

import android.app.Application;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.example.mytestdemo.HvacCallback;
import com.example.mytestdemo.HvacInterface;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tgw
 * @date 2022/11/23
 * @describe
 */
public class HvacManager extends BaseConnectManager<HvacInterface> {
    private static final String TAG = HvacManager.class.getSimpleName();
    private static volatile HvacManager sHvacManager;
    public static final String SERVICE_PACKAGE = "com.fwk.service";
    public static final String SERVICE_CLASSNAME =
            "com.fwk.service.SimpleService";
    private static final long RETRY_TIME = 5000L;
    private final List<IHvacCallback> mCallbacks = new ArrayList<>();
    private final HvacCallback.Stub mSampleCallback = new HvacCallback.Stub() {
        @Override
        public void onTemperatureChanged(double temperature) throws
                RemoteException {
            Log.d(TAG, "[onTemperatureChanged] " + temperature);
            getMainHandler().post(() -> {
                for (IHvacCallback callback : mCallbacks) {
                    callback.onTemperatureChanged(temperature);
                }
            });
        }
    };



    public static HvacManager getInstance() {
        if (sHvacManager == null) {
            synchronized (HvacManager.class) {
                if (sHvacManager == null) {
                    sHvacManager = new HvacManager();
                }
            }
        }
        return sHvacManager;
    }
    @Override
    protected String getServicePkgName() {
        return SERVICE_PACKAGE;
    }
    @Override
    protected String getServiceClassName() {
        return SERVICE_CLASSNAME;
    }
    @Override
    protected HvacInterface asInterface(IBinder service) {
        return HvacInterface.Stub.asInterface(service);
    }
    @Override
    protected long getRetryBindTimeMill() {
        return RETRY_TIME;
    }
    /******************/
    public void requestTemperature() {

        new Thread(new Runnable(){

            @Override
            public void run() {
                if (isServiceConnected(true)) {
                    try {
                        getProxy().requestTemperature();
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                } else {
// 将此方法放入队列中，等Service重新连接后，会依次调用
                    getTaskQueue().offer(HvacManager.this::requestTemperature);
                }
            }
        }).start();
//        Remote.tryExec(() -> {
//
//        });
    }
    public void setTemperature(int temperature) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (isServiceConnected(true)) {
                    try {
                        getProxy().requestTemperature();
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                } else {
                    getTaskQueue().offer(() -> {
                        setTemperature(temperature);
                    });
                }
            }
        }).start();
//        Remote.tryExec(() -> {
//
//        });
    }
//    public boolean registerCallback(IHvacCallback callback) {
//         new Thread(() -> {
//             if (isServiceConnected(true)) {
//                 boolean result = false;
//                 try {
//                     result = getProxy().registerCallback(mSampleCallback);
//                 } catch (RemoteException e) {
//                     e.printStackTrace();
//                 }
//                 if (result) {
//                     mCallbacks.remove(callback);
//                     mCallbacks.add(callback);
//                 }
//
//
//             } else {
//                 getTaskQueue().offer(() -> {
//                     registerCallback(callback);
//                 });
//
//             }
//         }).start();
////        return Remote.exec(() -> {
////            if (isServiceConnected(true)) {
////                boolean result = getProxy().registerCallback(mSampleCallback);
////                if (result) {
////                    mCallbacks.remove(callback);
////                    mCallbacks.add(callback);
////                }
////                return result;
////            } else {
////                getTaskQueue().offer(() -> {
////                    registerCallback(callback);
////                });
////                return false;
////            }
////        });
//    }


//    public boolean unregisterCallback(IHvacCallback callback) {
//        return Remote.exec(() -> {
//            if (isServiceConnected(true)) {
//                boolean result = getProxy().unregisterCallback(mSampleCallback);
//                if (result) {
//                    mCallbacks.remove(callback);
//                }
//                return result;
//            } else {
//                getTaskQueue().offer(() -> {
//                    unregisterCallback(callback);
//                });
//                return false;
//            }
//        });
//    }
}

